package com.xrz.sxm.aj.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xrz.sxm.aj.base.BaseApplication;
import com.xrz.sxm.aj.entity.BingDetail;
import com.xrz.sxm.aj.entity.ChanPinData;
import com.xrz.sxm.aj.entity.JingluoEntity;
import com.xrz.sxm.aj.entity.XueweiEntity;
import com.xrz.sxm.aj.entity.ZixunData;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class DBUtils {

	private SQLiteDatabase m_SqLiteDatabase;
	private Context m_Context;
	private static DBUtils m_DbUtils;

	public static DBUtils getInstant() {
		if (m_DbUtils == null)
			m_DbUtils = new DBUtils();
		return m_DbUtils;
	}

	private DBUtils() {
		m_Context = BaseApplication.ctx;
		m_SqLiteDatabase = new DBManager(m_Context).getDB();
	}

	/**
	 * 获取总资讯条数
	 */
	public int GetZiXunNums() {
		int count = 0;
		String sql = "select * from xwzxb";
		Cursor cursor = m_SqLiteDatabase.rawQuery(sql, null);
		count = cursor.getCount();
		cursor.close();
		return count;

	}

	/**
	 * 资讯列表
	 *
	 */
	public List<ZixunData> getZixunDatas(int startPage, int pageSize) {
		List<ZixunData> zixunDatas = new ArrayList<ZixunData>();
		ZixunData data;
		String sql = "select * from xwzxb limit " + startPage + "," + pageSize;
		Cursor cursor = m_SqLiteDatabase.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			data = new ZixunData();
			data.setUrl(cursor.getString(cursor.getColumnIndex("zxlj")));
			data.setImg(cursor.getString(cursor.getColumnIndex("zximg")));
			data.setTitle(cursor.getString(cursor.getColumnIndex("zxbt")));
			data.setContent(cursor.getString(cursor.getColumnIndex("zxjianjie")));
			zixunDatas.add(data);
		}
		cursor.close();
		return zixunDatas;
	}

	/**
	 * 获取典型案例的名字
	 */
	public List<String> getAnLiDatas() {
		List<String> Anlinames = new ArrayList<String>();
		String name;
		String sql = "select albt from dxalb";
		Cursor cursor = m_SqLiteDatabase.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			name = cursor.getString(cursor.getColumnIndex("albt"));
			Anlinames.add(name);
		}
		cursor.close();
		return Anlinames;
	}

	/**
	 * 根据典型案例的病例名获取内容实例
	 */
	public String getAnLicontent(String name) {
		String content = null;
		String sql = "select jtnr from dxalb where albt='" + name + "'";
		Cursor cursor = m_SqLiteDatabase.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			content = cursor.getString(cursor.getColumnIndex("jtnr"));
		}
		cursor.close();
		return content;
	}

	/**
	 * 获取产品的名字和图片
	 */

	public List<ChanPinData> GetChanPinDatas() {
		List<ChanPinData> datas = new ArrayList<ChanPinData>();
		String sql = "select * from cpmxb";
		Cursor cursor = m_SqLiteDatabase.rawQuery(sql, null);
		ChanPinData data;
		while (cursor.moveToNext()) {
			data = new ChanPinData();
			data.setName(cursor.getString(cursor.getColumnIndex("cpmc")));
			data.setJieshao(cursor.getString(cursor.getColumnIndex("cpjs")));
			data.setImgpath(cursor.getString(cursor.getColumnIndex("tplj")));
			datas.add(data);
		}
		cursor.close();
		return datas;
	}

	/**
	 * 根据身体部位获取病名列表
	 *
	 * @param buwei
	 * @return
	 */
	public List<String> getBingByBuwei(String buwei) {
		List<String> results = new ArrayList<String>();
		boolean isQuanshen = buwei.equals("全身");
		String sql;
		if (!isQuanshen)
			sql = "select * from stbm where bwmc='" + buwei + "'";
		else
			sql = "select * from stbm";
		Cursor cursor = m_SqLiteDatabase.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			results.add(cursor.getString(cursor.getColumnIndex("bm")));
		}
		cursor.close();
		return results;
	}

	/**
	 * 根据日期分类
	 *
	 * @param list
	 * @return
	 */
	private Map<Integer, List<XueweiEntity>> sort(List<XueweiEntity> list) {
		TreeMap tm = new TreeMap();
		for (int i = 0; i < list.size(); i++) {
			XueweiEntity s = (XueweiEntity) list.get(i);
			if (tm.containsKey(s.ajts)) {//
				ArrayList l11 = (ArrayList) tm.get(s.ajts);
				l11.add(s);
			} else {
				ArrayList tem = new ArrayList();
				tem.add(s);
				tm.put(s.ajts, tem);
			}
		}
		return tm;
	}

	/**
	 * 根据病名获取详细信息
	 *
	 * @param bingName
	 * @return
	 */
	public BingDetail getDetailByBing(String bingName) {
		BingDetail bingDetail = new BingDetail();
		List<XueweiEntity> list = new ArrayList<XueweiEntity>();
		Cursor cursor;
		String sql;
		sql = "SELECT * from sbgsb where bm='" + bingName + "'";
		String ajdm = "";
		cursor = m_SqLiteDatabase.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			bingDetail.bm = cursor.getString(cursor.getColumnIndex("bm"));
			bingDetail.gs = cursor.getString(cursor.getColumnIndex("gs"));
			bingDetail.byzz = cursor.getString(cursor.getColumnIndex("byzz"));
			bingDetail.byfx = cursor.getString(cursor.getColumnIndex("byfx"));
			bingDetail.ajth = cursor.getString(cursor.getColumnIndex("ajth"));
			bingDetail.tsbz = cursor.getString(cursor.getColumnIndex("tsbz"));
			bingDetail.sybz = cursor.getString(cursor.getColumnIndex("sybz"));
			ajdm = cursor.getString(cursor.getColumnIndex("ajdm"));
		}
		cursor.close();
		sql = "select * from ajmxb where ajdm='"+ajdm+"'";
		cursor = m_SqLiteDatabase.rawQuery(sql, null);
		XueweiEntity dbXueweiEntity = null;
		while(cursor.moveToNext()){
			int tianshu = cursor.getInt(cursor.getColumnIndex("ajts"));
			String [] wueweis = cursor.getString(cursor.getColumnIndex("xwdm")).split("、");
			for(String xuewei:wueweis){
				dbXueweiEntity = new XueweiEntity();
				dbXueweiEntity.ajts = tianshu;
				String strsql = "select * from xwmxb where xwmc='"+xuewei+"'";
				Cursor cursor2 = m_SqLiteDatabase.rawQuery(strsql, null);
				dbXueweiEntity.xwmc = xuewei;
				while(cursor2.moveToNext()){
					dbXueweiEntity.tmlj = cursor2.getString(cursor2
							.getColumnIndex("tmlj"));
					dbXueweiEntity.ajsj = cursor2.getString(cursor2
							.getColumnIndex("ajsj"));
					dbXueweiEntity.bz = cursor2.getString(cursor2.getColumnIndex("bz"));
					dbXueweiEntity.qxms = cursor2.getString(cursor2
							.getColumnIndex("qxms"));
					dbXueweiEntity.zzbz = cursor2.getString(cursor2
							.getColumnIndex("zzbz"));
					dbXueweiEntity.lcyy = cursor2.getString(cursor2
							.getColumnIndex("lcyy"));
					dbXueweiEntity.jldm = cursor2.getString(cursor2
							.getColumnIndex("jldm"));
				}
				list.add(dbXueweiEntity);
				cursor2.close();
			}
		}
		Map<Integer, List<XueweiEntity>> map = sort(list);
		bingDetail.quxues = map;
		cursor.close();
//		sql = "select xwmxb.*,ajmxb.ajts from xwmxb,ajmxb where ajmxb.ajdm='"
//				+ ajdm + "' and ajmxb.xwdm=xwmxb.xwdm;";
//		cursor = m_SqLiteDatabase.rawQuery(sql, null);
//		XueweiEntity dbXueweiEntity;
//		while (cursor.moveToNext()) {
//			dbXueweiEntity = new XueweiEntity();
//			dbXueweiEntity.xwmc = cursor.getString(cursor
//					.getColumnIndex("xwmc"));
//			dbXueweiEntity.tmlj = cursor.getString(cursor
//					.getColumnIndex("tmlj"));
//			dbXueweiEntity.ajsj = cursor.getString(cursor
//					.getColumnIndex("ajsj"));
//			dbXueweiEntity.bz = cursor.getString(cursor.getColumnIndex("bz"));
//			dbXueweiEntity.qxms = cursor.getString(cursor
//					.getColumnIndex("qxms"));
//			dbXueweiEntity.zzbz = cursor.getString(cursor
//					.getColumnIndex("zzbz"));
//			dbXueweiEntity.lcyy = cursor.getString(cursor
//					.getColumnIndex("lcyy"));
//			dbXueweiEntity.jldm = cursor.getString(cursor
//					.getColumnIndex("jldm"));
//			dbXueweiEntity.ajts = cursor.getInt(cursor.getColumnIndex("ajts"));
//			list.add(dbXueweiEntity);
//		}
//		cursor.close();
//
//		Map<Integer, List<XueweiEntity>> map = sort(list);
//		bingDetail.quxues = map;
//		cursor.close();
		return bingDetail;
	}

	/**
	 * 根据经络代码获取经络信息
	 *
	 * @param jinguodaima
	 * @return
	 */
	public JingluoEntity getJingluoByDaima(String jinguodaima) {
		String sql = "select * from jlb where jldm='" + jinguodaima + "'";
		Cursor cursor = m_SqLiteDatabase.rawQuery(sql, null);
		JingluoEntity entity = null;
		while (cursor.moveToNext()) {
			entity = new JingluoEntity();
			entity.jldm = cursor.getString(cursor.getColumnIndex("jldm"));
			entity.jlmc = cursor.getString(cursor.getColumnIndex("jlmc"));
			entity.ms = cursor.getString(cursor.getColumnIndex("ms"));
			entity.tplj = cursor.getString(cursor.getColumnIndex("tplj"));
		}
		cursor.close();
		return entity;
	}

	/**
	 * 根据分类名字获取病名
	 *
	 * @param text
	 * @return
	 */
	public List<String> getFenleiByName(String text) {
		List<String> results = new ArrayList<String>();
		String sql = "select DISTINCT bm from sbgsb where sybz='" + text + "'";
		Cursor cursor = m_SqLiteDatabase.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			results.add(cursor.getString(cursor.getColumnIndex("bm")));
		}
		cursor.close();
		return results;
	}

	/**
	 * 根据特殊标记获取病名
	 *
	 * @param teshu
	 * @return
	 */
	public List<String> getFenleiByTeShu(String teshu) {
		List<String> results = new ArrayList<String>();
		String sql = "select DISTINCT bm from sbgsb where tsbz='" + teshu + "'";
		Cursor cursor = m_SqLiteDatabase.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			results.add(cursor.getString(cursor.getColumnIndex("bm")));
		}
		cursor.close();
		return results;
	}

	public List<String> search(String key) {
		List<String> results = new ArrayList<String>();
		String sql = "select bm from sbgsb where bm like '%" + key + "%'";
		Cursor cursor = m_SqLiteDatabase.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			results.add(cursor.getString(cursor.getColumnIndex("bm")));
		}
		cursor.close();
		return results;
	}

	/**
	 * 搜索的获取病名列表
	 */
	public List<String> getKeys() {
		List<String> results = new ArrayList<String>();
		String sql = "select bm from sbgsb";
		Cursor cursor = m_SqLiteDatabase.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			results.add(cursor.getString(cursor.getColumnIndex("bm")));
		}
		cursor.close();
		return results;
	}
}
